package com.junzhiit.nengji.cms.service.impl;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.junzhiit.nengji.cms.service.SystemConfigService;
import com.junzhiit.nengji.cms.web.vo.ResultVO;
import com.junzhiit.nengji.cms.web.vo.SystemConfigEditVO;
import com.junzhiit.nengji.dao.GoodsDAO;
import com.junzhiit.nengji.dao.ShopDAO;
import com.junzhiit.nengji.dao.SystemConfigDAO;
import com.junzhiit.nengji.model.SystemConfig;
import com.junzhiit.nengji.model.enums.Is;

@Service
public class SystemConfigServiceImpl extends BaseServiceImpl implements SystemConfigService {
	
	@Autowired
	private SystemConfigDAO systemConfigDAO;
	
	@Autowired
	private GoodsDAO goodsDAO;
	
	@Autowired
	private ShopDAO  shopDAO;
	
	@Override
	public ResultVO find(){
		ResultVO result=new ResultVO();
		SystemConfig data=systemConfigDAO.find();
		result.setData(data);
		return result;
	}
	
	@Override
	@Transactional
	public ResultVO update(SystemConfigEditVO systemConfigEditVO){
		ResultVO result=new ResultVO();
		SystemConfig systemConfig=new SystemConfig();
		BeanUtils.copyProperties(systemConfigEditVO, systemConfig);
		if(systemConfigEditVO.getIsOpenRebate() == Is.NO.toIntValue()){
			goodsDAO.updateIsOpenRebate();
			shopDAO.updateIsOpenRebate();
		}
		systemConfigDAO.update(systemConfig);
		result.setData(systemConfig);
		return result;
	}
}
